<template>
  <el-dialog :title="formData.id?'编辑客户':'添加客户'" :visible="showDialog" @close="closeFn">
    <el-form ref="form" label-width="60px" :model="formData" :rules="rules">
      <el-form-item label="经纪" prop="agent">
        <el-select v-model="formData.agent">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="formData.name" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="formData.email" />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="formData.phone" />
      </el-form-item>
      <el-form-item label="微信" prop="wechat">
        <el-input v-model="formData.wechat" />
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input v-model="formData.desc" type="textarea" />
      </el-form-item>
      <el-form-item label="封面" prop="image">
        <!-- 图片上传 -->
        <UploadImg />
        <el-input v-model="formData.image" placeholder="强制设置图片跳过上传" />
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-row type="flex" justify="end">
        <el-button type="primary" size="small" @click="submit">确定</el-button>
        <el-button size="small" @click="closeFn">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { agentsList } from '@/api/agents'
import { addClientsList, getClientsDetail, editClients } from '@/api/clients'
export default {
  props: {
    // 显示弹窗
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      options: [], // 经纪数组
      value: '', // 下拉列表默认值
      formData: { // 表单数据
        agent: '',
        name: '',
        email: '',
        phone: '',
        wechat: '',
        desc: '',
        image: ''
      },
      // 校验规则
      rules: {
        name: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        desc: [{ required: true, message: '描述不能为空', trigger: 'blur' }]
      }

    }
  },
  async created() {
    // 获取下拉列表经纪的数据
    const res = await agentsList()
    this.options = res.data
  },
  methods: {
    // 关闭弹窗
    closeFn() {
      this.$emit('update:showDialog', false)
      // 清空数据
      this.formData = {
        agent: '',
        name: '',
        email: '',
        phone: '',
        wechat: '',
        desc: '',
        image: ''
      }
      // 关闭错误校验
      this.$refs.form.resetFields()
    },
    // 新建客户
    async submit() {
      // 表单校验
      await this.$refs.form.validate()
      if (this.formData.id) {
        // 编辑数据
        await editClients(this.formData)
      } else {
        // 添加数据
        await addClientsList(this.formData)
      }
      // 刷新列表
      this.$emit('updateEvent')
      // 关闭弹窗
      this.closeFn()
    },
    // 获取客户详情
    async getClientsDetail(id) {
      const res = await getClientsDetail(id)
      // 回显
      this.formData = res.data
    }
  }
}
</script>

<style lang="scss" scoped >
::v-deep .el-textarea__inner{
    height: 100px;
}
</style>
